use aes::Aes128;
use block_modes::{BlockMode, Cbc};
use block_modes::block_padding::Pkcs7;
use napi_derive_ohos::napi;
use napi_ohos::bindgen_prelude::*;

type Aes128Cbc = Cbc<Aes128, Pkcs7>;

#[napi]
pub fn aes_encrypt(input: Buffer, key: Buffer, iv: Buffer) -> Result<Buffer> {
    let cipher = Aes128Cbc::new_from_slices(&key, &iv).map_err(|_| Error::from_reason("加密器创建失败"))?;

    let encrypted_data = cipher.encrypt_vec(&input);
    Ok(Buffer::from(encrypted_data))
}

#[napi]
pub fn aes_decrypt(input: Buffer, key: Buffer, iv: Buffer) -> Result<Buffer> {
    let cipher = Aes128Cbc::new_from_slices(&key, &iv).map_err(|_| Error::from_reason("加密器创建失败"))?;

    let decrypted_data = cipher.decrypt_vec(&input).map_err(|_| Error::from_reason("解密失败"))?;
    Ok(Buffer::from(decrypted_data))
}

